package com.itluma.isWOak.service.impl;

import com.itluma.isWOak.mapper.AdminRoleMapper;
import com.itluma.isWOak.pojo.AdminInfo;
import com.itluma.isWOak.pojo.FunctionInfo;
import com.itluma.isWOak.pojo.RoleInfo;
import com.itluma.isWOak.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author MS
 * @date 2021/4/8 14:29
 **/

@Service
public class AdminRoleServiceImpl implements AdminRoleService{

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    /**
     *
     * @param admin
     * @return
     * 根据用户查找所有的角色
     */
    @Override
    public List<RoleInfo> findByAdmin(AdminInfo admin){
        if (admin != null) {
           return adminRoleMapper.findRoleByAid (admin.getAdminId ());
        }
        return null;
    }

    @Override
    public List<FunctionInfo> findbyAdmin(AdminInfo admin){
        if (admin != null) {
            return adminRoleMapper.findFunctionByAid(admin.getAdminId ());
        }
        return null;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyAdminRole(Integer adminId, Integer... roleIds) {
        adminRoleMapper.deleteByAdminId(adminId);
        for(int i = 0; i < roleIds.length; i++){
            adminRoleMapper.insert(adminId, roleIds[i]);
        }
    }
}

